<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\api\validate;

use app\common\model\Channel;
use taoser\Validate;
use app\common\model\Goods;

class OrderValidate extends Validate
{
    protected $goods = null;

    protected $rule = [
        'goods_id' => 'require|checkGoodsExists|checkGoodsStatus',
        'pid'      => 'require|checkPid',
    ];

    protected $message = [
        'goods_id.require'          => '商品选择错误！',
        'goods_id.checkGoodsExists' => 'goods_not_exists',
        'goods_id.checkGoodsStatus' => '该商品已下架！',
        'pid.require'               => '请选择付款方式！',
        'pid.checkPid'              => '付款方式选择错误！',
    ];

    // 定义验证方法
    protected function checkGoodsExists($value, $rule, $data = [])
    {
        $this->goods = Goods::where(["id" => $value])->find();
        return $this->goods ? true : false;
    }


    protected function checkGoodsStatus($value, $rule, $data = [])
    {
        $goods = Goods::where(["id" => $value])->find();
        return $goods->status === 1 ? true : false;
    }

    // 支付方式检测
    protected function checkPid($value, $rule, $data = [])
    {
        $channel = Channel::where(["id" => $value])->find();
        if (empty($channel)) {
            return '支付方式不存在';
        }
        if ($channel->status != 1) {
            return '支付方式已关闭【系统关闭】，请切换其他支付方式';
        }
        return true;
    }


    // 创建订单时的 验证场景
    public function sceneCreate()
    {
        $create = ['goods_id', 'pid'];
        return $this->only($create);
    }

}
